<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/5/26
 * Time: 16:12
 */

namespace app\admin\controller;
use app\admin\Common;
use app\admin\Model;
use think\Response;
use think\Request;
class Member extends Base
{

    public $state ;
    /*
     * 分页显示会员列表
     * */
    public  function member(){
        $condition=null;
        $category=null;
        $mobile=null;
        if (Request::instance()->isPost()){
            if(Request::instance()->post('category')!='all'){
                $condition['member_category']=Request::instance()->post('category');
                $category=$condition['member_category'];
            }
            if(Request::instance()->post('mobile')!=''){
                $condition['member_mobile']=Request::instance()->post('mobile');
                $mobile=$condition['member_mobile'];
            }
        }
       // dump($category);exit;
        $list=Model\Member::show($condition);
        $data=$list->items();
        foreach($data as $key=>$value){
            $data[$key]=Common::Alias($value);
            $data[$key]['member_created_at']=date('Y-m-d H:i',$value['member_created_at']);
        }
        return view('list',
            [
                'mobile'=>$mobile,
                'category'=>$category,
                'data'=>$data,
                'list'=>$list,
            ]);
    }

    /*
     * 修改拨打状态
     * */
    public function  changeState(){
        if(request()->isAjax()){
            $condition['member_id']=htmlspecialchars($this->request->post('id'));
            $state=Model\Member::getField($condition,'member_state');
            if($state==10){
                $data['member_state']=30;
                $this->state='未拨打';

            }else if($state==30){
                $data['member_state']=10;
                $this->state='已拨打';

            }else{
                return '查询失败';
            }
            $res=Model\Member::change($condition,$data);
            if($res){
                return $this->state;
            }else{
                return '修改失败';
            }
        }else{
            $this->error('错误页面');
        }
    }

    /*
     *删除
     * */
    public  function del(){
        if(request()->isAjax()){
            $condition['member_id']=htmlspecialchars(request()->post('id'));
            $res=Model\Member::deleted($condition);
            return $res;
        }else{
            $this->error('错误页面');
        }

    }

    /*
     * 编辑查看会员信息
     * */

    public function edit(){
        $condition['member_id']=htmlspecialchars($this->request->get('id'));
        $list=Model\Member::getField($condition);
        return view('edit',
            ['list'=>$list,]
            );
    }

    /*
     * 处理提交的编辑内容
     * */
    public function doEdit(){
        if(request()->isAjax()){
           // return 1;exit;
            $data=Request::instance()->post() ;
            $condition['member_id']=$data['member_id'];
            $list=Model\Member::getField($condition);
            $dat=array_diff_assoc($data,$list);
            //return $dat;exit;
            if(empty($dat)){
                return true;exit;
            }
            $res=Model\Member::change($condition,$data);
            if($res){
                return true;
            }else{
                return false;
            }
        }else{
            $this->error('请求错误');
        }
    }

}